package cc.rengu.igas.channel.uprk.biz.impl;

import cc.rengu.igas.channel.uprk.common.constant.UprkServiceConstant;
import cc.rengu.igas.channel.uprk.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.uprk.facade.UprkBankCardShareQueryService;
import cc.rengu.igas.channel.uprk.facade.request.BankCardShareGenTokenRequest;
import cc.rengu.igas.channel.uprk.facade.request.BankCardShareMchntVerifyRequest;
import cc.rengu.igas.channel.uprk.facade.request.BankCardShareMobileWebVerifyRequest;
import cc.rengu.igas.channel.uprk.facade.request.BankCardSharePersonalVerifyRequest;
import cc.rengu.igas.channel.uprk.facade.response.BankCardShareGenTokenResponse;
import cc.rengu.igas.channel.uprk.facade.response.BankCardShareMchntVerifyResponse;
import cc.rengu.igas.channel.uprk.facade.response.BankCardShareMobileWebVerifyResponse;
import cc.rengu.igas.channel.uprk.facade.response.BankCardSharePersonalVerifyResponse;
import cc.rengu.igas.channel.uprk.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * @author wukefan
 * @version 1.0.0
 * @date 2021/4/25 15:10
 */
public class UprkBankCardShareQueryServiceImpl implements UprkBankCardShareQueryService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /**
     * 获取产品TOKEN
     *
     * @param bankCardShareGenTokenRequest
     * @return Result<BankCardShareGenTokenResponse>
     */
    @Override
    public Result<BankCardShareGenTokenResponse> bankCardShareGenToken(BankCardShareGenTokenRequest bankCardShareGenTokenRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BankCardShareGenTokenResponse> result = new Result<>();
        BankCardShareGenTokenResponse creditTransStateQueryRespone = new BankCardShareGenTokenResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(bankCardShareGenTokenRequest,
                    UprkServiceConstant.UPRK_BANK_CARD_SHARE_GEN_TOKEN, BankCardShareGenTokenResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BankCardShareGenTokenResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("获取银联风险产品token失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                creditTransStateQueryRespone.setRespMsg(((BizException) e).getCode());
                creditTransStateQueryRespone.setRespMsg(e.getMessage());
            } else {
                creditTransStateQueryRespone.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                creditTransStateQueryRespone.setRespMsg(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 个人风险信息验证
     *
     * @param bankCardSharePersonalVerifyRequest
     * @return Result<BankCardSharePersonalVerifyResponse>
     */
    @Override
    public Result<BankCardSharePersonalVerifyResponse> bankCardSharePersonalVerify(BankCardSharePersonalVerifyRequest bankCardSharePersonalVerifyRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BankCardSharePersonalVerifyResponse> result = new Result<>();
        BankCardSharePersonalVerifyResponse bankCardSharePersonalVerifyResponse = new BankCardSharePersonalVerifyResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(bankCardSharePersonalVerifyRequest,
                    UprkServiceConstant.UPRK_BANK_CARD_SHARE_PERSONAL_VERIFY, BankCardSharePersonalVerifyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BankCardSharePersonalVerifyResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("个人风险信息验证失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                bankCardSharePersonalVerifyResponse.setRespMsg(((BizException) e).getCode());
                bankCardSharePersonalVerifyResponse.setRespMsg(e.getMessage());
            } else {
                bankCardSharePersonalVerifyResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                bankCardSharePersonalVerifyResponse.setRespMsg(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 商户风险信息验证
     *
     * @param bankCardShareMchntVerifyRequest
     * @return Result<BankCardShareMchntVerifyResponse>
     */
    @Override
    public Result<BankCardShareMchntVerifyResponse> bankCardShareMchntVerify(BankCardShareMchntVerifyRequest bankCardShareMchntVerifyRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BankCardShareMchntVerifyResponse> result = new Result<>();
        BankCardShareMchntVerifyResponse bankCardShareMchntVerifyResponse = new BankCardShareMchntVerifyResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(bankCardShareMchntVerifyRequest,
                    UprkServiceConstant.UPRK_BANK_CARD_SHARE_MCHNT_VERIFY, BankCardShareMchntVerifyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BankCardShareMchntVerifyResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户风险信息验证失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                bankCardShareMchntVerifyResponse.setRespMsg(((BizException) e).getCode());
                bankCardShareMchntVerifyResponse.setRespMsg(e.getMessage());
            } else {
                bankCardShareMchntVerifyResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                bankCardShareMchntVerifyResponse.setRespMsg(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 移动互联网风险信息验证
     *
     * @param bankCardShareMobileWebVerifyRequest
     * @return Result<BankCardShareMobileWebVerifyResponse>
     */
    @Override
    public Result<BankCardShareMobileWebVerifyResponse> bankCardShareMobileWebVerify(BankCardShareMobileWebVerifyRequest bankCardShareMobileWebVerifyRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<BankCardShareMobileWebVerifyResponse> result = new Result<>();
        BankCardShareMobileWebVerifyResponse bankCardShareMobileWebVerifyResponse = new BankCardShareMobileWebVerifyResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(bankCardShareMobileWebVerifyRequest,
                    UprkServiceConstant.UPRK_BANK_CARD_SHARE_MOBILE_WEB_VERIFY, BankCardShareMobileWebVerifyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((BankCardShareMobileWebVerifyResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("移动互联网风险信息验证失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                bankCardShareMobileWebVerifyResponse.setRespMsg(((BizException) e).getCode());
                bankCardShareMobileWebVerifyResponse.setRespMsg(e.getMessage());
            } else {
                bankCardShareMobileWebVerifyResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                bankCardShareMobileWebVerifyResponse.setRespMsg(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
